﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace SoundReg
{
    class RegData
    {
        public RegData() { }
        

        /** 注册表路径地址组 **/
        private static string[] urlGroup = new String[]{};
        /** 声音地址路径 **/
        public bool isRegSuccess = true;

        public RegistryKey mainSource = Registry.CurrentUser;

        /** 注册表路径 **/
        private string regUrl = String.Empty;
        public string RegUrl 
        {
            get { return regUrl; }
            set
            {
                regUrl = value;
            }
        }

        /** 注册表路径转换 **/
        public void GetRegPathGroup(string url)
        {
            if (url.StartsWith("[")) 
            {
                if (url.IndexOf("]")> 0)
                {
                    url = url.Substring(1, url.IndexOf("]") - 2);
                }
                else
                {
                    url = url.Substring(1, url.Length - 1);
                }
            }
            // 统一路径格式
            url = url.Replace("\\","/");
            urlGroup = url.Split('/');
        }  

        /** 注册表路径转换 **/
        private RegistryKey GetRegRegistryHeader(string name, ref bool vaild)
        {
            RegistryKey mainSource = Registry.CurrentUser ;
            switch (name) {
                case "HKEY_CLASSES_ROOT":
                    mainSource = Registry.ClassesRoot;
                    break;
                case "HKEY_CURRENT_CONFIG":
                    mainSource = Registry.CurrentConfig;
                    break;
                case "HKEY_CURRENT_USER":
                    mainSource =  Registry.CurrentUser;
                    break;
                case "HKEY_DYN_DATA":
                    mainSource = Registry.DynData;
                    break;
                case "HKEY_LOCAL_MACHINE":
                    mainSource = Registry.LocalMachine;
                    break;
                case "HKEY_PERFORMANCE_DATA":
                    mainSource = Registry.PerformanceData;
                    break;
                case "HKEY_USERS":
                    mainSource = Registry.Users;
                    break;
               default:
                    break;
            }
            return mainSource;
        }

        /**
           * \brief 创建各设备类的存储字典
           * 
           * \param[in] string 设备类型，对应Reg中的summary
           */
        public bool SetRegRegistryKey()
        {
                bool vaild = true;
                RegistryKey mainSource = GetRegRegistryHeader(urlGroup[0], ref vaild);
                RegistryKey lastRegKey = mainSource;
                RegistryKey curreentRegKey = mainSource;
                if (vaild) 
                {
                    try
                    {
                        for (int i = 1; i < urlGroup.Length; i++)
                        {
                            curreentRegKey = lastRegKey.OpenSubKey(urlGroup[i], true);
                            lastRegKey = curreentRegKey;
                        }
                        mainSource.SetValue("value", RegUrl);
                    }
                    catch
                    {
                        vaild = false;
                    }
                }
                return vaild;
         }
    }


    class RegManager
    {
        /**
          * \brief 创建各设备类的存储字典
          * 
          * \param[in] string 设备类型，对应Reg中的summary
          */
        private  Dictionary<string, string> MainRegDic = new Dictionary<string, string>()
        {
                           {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/ChangeTheme/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\桌面主题已更改完毕.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/DeviceFail/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\提示：设备连接失败.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/DeviceDisconnect/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\提示：设备连接已中断.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/DeviceConnect/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\外接设备已连接成功 .wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/WindowsLogon/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\欢迎使用史柯凡智能安全防护系统.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/WindowsLogoff/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\下次见.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/AppGPFault/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\应用程序向您提出权限申请.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/SystemHand/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\警告，软件出现关键性停止.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/LowBatteryAlarm/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\警告，电量仅剩下百分之十，设备大约还可以持续工作十五分钟左右，请确认您附近是否存在可用插销，以便及时充电.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/CriticalBatteryAlarm/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\警告，电量严重不足，请及时充电.wav"},
                                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/MailBeep/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\您有一份新邮件.wav"},
                //            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/Notification.Mail/.Current ]", 
                //System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\您有一份新邮件.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/Notification.Reminder/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\先生，您有一条系统消息.wav"},
                //            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/ChangeTheme/.Current ]", 
                //System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\智能语音识别程序已启动.wav"},
                //            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/ChangeTheme/.Current ]", 
                //System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\语音识别已关闭.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/SystemQuestion/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\警告.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/SnapShot]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\全屏截图已完毕.wav"}
        };

        /**
           * \brief 创建各设备类的存储字典
           * 
           * \param[in] string 设备类型，对应Reg中的summary
           */
        private Dictionary<string, string> RetSetRegDic = new Dictionary<string, string>()
        {
                           {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/ChangeTheme/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\桌面主题已更改完毕.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/DeviceFail/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\提示：设备连接失败.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/DeviceDisconnect/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\提示：设备连接已中断.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/DeviceConnect/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\外接设备已连接成功 .wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/WindowsLogon/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\欢迎使用史柯凡智能安全防护系统.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/WindowsLogoff/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\下次见.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/AppGPFault/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\应用程序向您提出权限申请.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/SystemHand/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\警告，软件出现关键性停止.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/LowBatteryAlarm/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\警告，电量仅剩下百分之十，设备大约还可以持续工作十五分钟左右，请确认您附近是否存在可用插销，以便及时充电.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/CriticalBatteryAlarm/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\警告，电量严重不足，请及时充电.wav"},
                                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/MailBeep/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\您有一份新邮件.wav"},
                //            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/Notification.Mail/.Current ]", 
                //System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\您有一份新邮件.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/Notification.Reminder/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\先生，您有一条系统消息.wav"},
                //            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/ChangeTheme/.Current ]", 
                //System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\智能语音识别程序已启动.wav"},
                //            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/ChangeTheme/.Current ]", 
                //System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\语音识别已关闭.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/SystemQuestion/.Current ]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\警告.wav"},
                            {"[HKEY_CURRENT_USER/AppEvents/Schemes/Apps/.Default/SnapShot]", 
                System.AppDomain.CurrentDomain.BaseDirectory + @"Lily\全屏截图已完毕.wav"}
        };

        /** 注册表信息列表 **/
        public  List<RegData> regList = new List<RegData>();

        /** 注册表数据解析 **/
        public void SetRegData()
        {
            regList.Clear();
            foreach (var v in MainRegDic)
            {
                RegData reg = new RegData() { RegUrl = v.Value};
                // 路径分割与解析
                reg.GetRegPathGroup(v.Key);
                // 进行相关注册工作
                reg.isRegSuccess= reg.SetRegRegistryKey();
                regList.Add(reg);
            }
        }
    }
}
